﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Ninject;
using SydEnergiPrototype.DataAccess;
using SydEnergiPrototype.Logic;
using SydEnergiPrototype.Model;

namespace SydEnergiPrototype.ViewModel
{
    public class AddDataViewModel : ViewModelBase, IDataViewModel
    {
        #region Members

        private List<SolidColorBrush> _colorCodes;
        public List<SolidColorBrush> ColorCodes
        {
            get
            {
                if (_colorCodes == null)
                {
                    _colorCodes = _colorCodeDb.GetColorCodes();
                }
                return _colorCodes;
            }
            set { ColorCodes = value; Notify("ColorCodes"); }
        }

        //private string _streetName;
        //public string StreetName
        //{
        //    get { return _streetName; }
        //    set { _streetName = value; Notify("StreetName"); }
        //}

        //private string _streetNumber;
        //public string StreetNumber
        //{
        //    get { return _streetNumber; }
        //    set { _streetNumber = value; Notify("StreetNumber"); }
        //}

        //private int _zipCode;
        //public int ZipCode
        //{
        //    get { return _zipCode; }
        //    set { _zipCode = value; Notify("ZipCode"); }
        //}

        //private string _city;
        //public string City
        //{
        //    get { return _city; }
        //    set { _city = value; Notify("City"); }
        //}

        //private string _letter;
        //public string Letter
        //{
        //    get { return _letter; }
        //    set { _letter = value; Notify("Letter"); }
        //}

        //private int _scposSelectedIndex;
        //public int ScPosSelectedIndex
        //{
        //    get { return _scposSelectedIndex; }
        //    set { _scposSelectedIndex = value; Notify("ScPosSelectedIndex"); }
        //}

        public List<string> StaircasePosition
        {
            get
            {
                var type = typeof(ScPos);
                if (!type.IsEnum)
                    throw new ArgumentException("Type '" + type.Name + "' is not an enum");

                var fields = (from field in type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
                              where field.IsLiteral
                              select field.Name).ToList<string>();

                return fields;
            }
        }
        
        //private string _floor;
        //public string Floor
        //{
        //    get { return _floor; }
        //    set { _floor = value; Notify("Floor"); }
        //}
        //private int _cassette;
        //public int Cassette
        //{
        //    get { return _cassette; }
        //    set { _cassette = value; Notify("Cassette"); }
        //}

        //private int _position;
        //public int Position
        //{
        //    get { return _position; }
        //    set { _position = value; Notify("Position"); }
        //}

        //private int _fiberNumber;
        //public int FiberNumber
        //{
        //    get { return _fiberNumber; }
        //    set { _fiberNumber = value; Notify("FiberNumber"); }
        //}
        //private string _cable;
        //public string Cable
        //{
        //    get { return _cable; }
        //    set { _cable = value; Notify("Cable"); }
        //}
        //private string _teletube;
        //public string Teletube
        //{
        //    get { return _teletube; }
        //    set { _teletube = value; Notify("Teletube"); }
        //}
        //private int _innerTube;
        //public int InnerTube
        //{
        //    get { return _innerTube; }
        //    set { _innerTube = value; Notify("InnerTube"); }
        //}
        //private string _innerTubeId;
        //public string InnerTubeId
        //{
        //    get { return _innerTubeId; }
        //    set { _innerTubeId = value; Notify("InnerTubeId"); }
        //}
        //private string _lssemb;
        //public string Lssemb
        //{
        //    get { return _lssemb; }
        //    set { _lssemb = value; Notify("Lssemb"); }
        //}



        private Entry _currentEntry;
        public Entry CurrentEntry
        {
            get { return _currentEntry; }
            set { _currentEntry = value; Notify("CurrentEntry"); }
        }

        private RelayCommand _saveEntryCommand;
        public RelayCommand SaveEntryCommand
        {
            get
            {
                if (_saveEntryCommand == null)
                {
                    _saveEntryCommand = new RelayCommand(param => this.AddData());
                }
                return _saveEntryCommand;
            }
        }

        private RelayCommand _clearFieldsCommand;
        public RelayCommand ClearFieldsCommand
        {
            get 
            {
                if(_clearFieldsCommand == null)
                {
                    _clearFieldsCommand = new RelayCommand(param => this.ClearFields());
                }
                return _clearFieldsCommand;
            }
        }

        public string Header
        {
            get { throw new NotImplementedException(); }
        }

        private int _fiberSelectedIndex;
        public int FiberSelectedIndex
        {
            get { return _fiberSelectedIndex; }
            set { _fiberSelectedIndex = value; Notify("FiberSelectedIndex"); }
        }

        private int _tubeSelectedIndex;
        public int TubeSelectedIndex
        {
            get { return _tubeSelectedIndex; }
            set { _tubeSelectedIndex = value; Notify("TubeSelectedIndex"); }
        }

        private readonly IMemoryDataStore _memoryDataStore;
        private readonly IColorCodeDB _colorCodeDb;

        #endregion

        [Inject]
        public AddDataViewModel(IMemoryDataStore memoryDataStore, IColorCodeDB colorCodeDb)
        {
            if(memoryDataStore == null)
                throw new ArgumentNullException("memoryDataStore");

            _memoryDataStore = memoryDataStore;

            if(colorCodeDb == null)
                throw new ArgumentNullException("colorCodeDb");

            _colorCodeDb = colorCodeDb;
            
            InitializeMembers();
        }

        private void InitializeMembers()
        {
            FiberSelectedIndex = 0;
            TubeSelectedIndex = 0;
        }

        private void AddData()
        {
            //var entry = ExtractEntryFromFields();
            //_memoryDataStore.Persist(entry);
        }

        private void ClearFields()
        {
            _currentEntry = new Entry();
            FiberSelectedIndex = 0;
            TubeSelectedIndex = 0;
        }


        //private Entry ExtractEntryFromFields()
        //{
        //    var entry = new Entry();
        //    entry.EntryAddress = new Address(StreetName, Convert.ToInt32(StreetNumber), Letter, Floor, (ScPos)ScPosSelectedIndex, ZipCode, City);
        //    entry.Fiber = ColorCodes[FiberSelectedIndex];
        //    entry.Tube = ColorCodes[TubeSelectedIndex];
        //    entry.Cable = Cable;
        //    entry.Cassette = Cassette;
        //    entry.FiberNumber = FiberNumber;
        //    entry.InnerTube = InnerTube;
        //    entry.InnerTubeId = InnerTubeId;
        //    entry.LsseMb = Lssemb;
        //    entry.Position = Position;
        //    entry.Teletube = Teletube;

        //    return entry;
        //}
    }
}
